//
//  GoldPriceCell.m
//  PriceOnline
//
//  Created by Le Anh Tai on 7/22/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "GoldPriceCell.h"
#import "UIConstants.h"
#import "Utility.h"
#import <QuartzCore/QuartzCore.h>
#import "Global.h"

@implementation GoldPriceCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {        
        _codeLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(14) textColor:_textColor];
        [_subView addSubview:_codeLabel];
        _codeLabel.font = FONT_HELVETICANEUE(GOLD_TYPE_FONT_SIZE);
        _codeLabel.lineBreakMode = UILineBreakModeWordWrap;
        _codeLabel.numberOfLines = 2;
        
        _buyLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(14) textColor:_textColor];
        [_subView addSubview:_buyLabel];
        
        _changedBuyLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(12) textColor:_textColor];
        _changedBuyLabel.numberOfLines = 2;
        [_subView addSubview:_changedBuyLabel];
        
        _sellLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(14) textColor:_textColor];
        [_subView addSubview:_sellLabel];
        
        _changedSellLabel = [Utility createLabelWithFont:FONT_HELVETICANEUE(12) textColor:_textColor];
        _changedSellLabel.numberOfLines = 2;
        [_subView addSubview:_changedSellLabel];
        
        _buyLabel.textAlignment = _changedBuyLabel.textAlignment = _changedSellLabel.textAlignment = _sellLabel.textAlignment = NSTextAlignmentRight;
    }
    
    return self;
}

- (void)reloadTheme
{
    [super reloadTheme];
    
    _codeLabel.textColor = _textColor;
    _buyLabel.textColor = _textColor;
    _sellLabel.textColor = _textColor;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger paddingRight = 20;
    NSInteger typeWid = 3*(self.contentView.width - paddingRight)/5;
    NSInteger priceWid = (self.contentView.width - paddingRight)/5;
    NSInteger paddingTop = 4;
        
    _codeLabel.frame = RECT(0, 0, typeWid, self.height);
    
    _buyLabel.frame = RECT(_codeLabel.right, 0, priceWid, self.height/3 + paddingTop);
    _changedBuyLabel.frame = RECT(_codeLabel.right, _buyLabel.bottom, priceWid, self.height-_buyLabel.height - paddingTop);
    
    _sellLabel.frame = RECT(_buyLabel.right, 0, self.contentView.width - _buyLabel.right - paddingRight, self.height/3 + paddingTop);
    _changedSellLabel.frame = RECT(_buyLabel.right, _sellLabel.bottom, _sellLabel.width, self.height-_sellLabel.height - paddingTop);
}

- (void)loadDataWithDictionary:(NSDictionary*)dict
{
    [self setLable:[dict objectForKey:GOLD_CODE_KEY] forLabel:_codeLabel];

    //
    NSString *value = [dict objectForKey:GOLD_BUY_KEY];
    float fValue = [Utility convertStringToFloat:value];
    [self setLable:value forLabel:_buyLabel];
    
    NSString *preValue = [dict objectForKey:PRE_GOLD_BUY_KEY];
    float fPreValue = [Utility convertStringToFloat:preValue];
#ifdef __DUMMY_DATA__
    fPreValue = fValue + .3f;
#endif

    UIColor *textColor = COLOR_GREEN_NORMAL;
    
    if (fValue < fPreValue)
    {
        textColor = COLOR(255, 19, 20, 0.9);
    }
    
    BOOL is2Lines = IS_IPHONE;
    
    [_changedBuyLabel setTextColor:textColor];
    [_changedBuyLabel setText:[Utility makeChangedString:fValue
                                                preValue:fPreValue
                                         ignoreZeroValue:YES
                                    changedPerInDiffLine:is2Lines]];
    
    //
    value = [dict objectForKey:GOLD_SELL_KEY];
    fValue = [Utility convertStringToFloat:value];
    [self setLable:value forLabel:_sellLabel];
    
    preValue = [dict objectForKey:PRE_GOLD_SELL_KEY];
    fPreValue = [Utility convertStringToFloat:preValue];
#ifdef __DUMMY_DATA__
    fPreValue = fValue - .3f;
#endif

    textColor = COLOR_GREEN_NORMAL;
    
    if (fValue < fPreValue)
    {
        textColor = COLOR(255, 19, 20, 0.9);
    }
    
    [_changedSellLabel setTextColor:textColor];
    [_changedSellLabel setText:[Utility makeChangedString:fValue
                                                 preValue:fPreValue
                                          ignoreZeroValue:YES
                                     changedPerInDiffLine:is2Lines]];
}
@end
